//
//  QuestionBankViewController.m
//  jinnengExam
//
//  Created by mac on 2020/8/12.
//  Copyright © 2020 jznrj. All rights reserved.
//

#import "QuestionBankViewController.h"
#import "QuestionBrowseViewController.h"
#import "PBFlatBarButtonItems.h"


@interface QuestionBankViewController ()

@property(nonatomic) int deep;


@end

@implementation QuestionBankViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIBarButtonItem *back = [PBFlatBarButtonItems backBarButtonItemWithTarget:self selector:@selector(goBack)];
    [self.navigationItem setLeftBarButtonItem:back];
    self.title = @"题库选择";
    self.deep = 0;
    [self initView];
}


-(void) viewWillAppear:(BOOL)animated{

}

-(void) initView{
   [self.view addSubview:self.jznWebView];
   [self.jznWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        if (@available(iOS 11.0, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.top.bottom.equalTo(self.view);
        }
    }];
    self.jznWebView.scrollView.bounces = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    [self.jznWebView loadLocalFile:@"/ios_jinneng/question_bank" type:@"html"];
    
    [self registerFunction];
}
-(void) registerFunction{
    

//    [self.webViewBridge registerHandler:@"goBack" handler:^(id data, WVJBResponseCallback responseCallback) {
//        if (self.deep>1) {
//            [self findParentBankList];
//            return;
//        }
//        [self.navigationController popViewControllerAnimated:YES];
//    }];
    
    [self.webViewBridge registerHandler:@"chooseQuestionBank" handler:^(id data, WVJBResponseCallback responseCallback) {
        self.questionBankId = [data[@"questionBankId"] intValue];
        [self findQuestionBankByParent];
    }];
}

-(void) findQuestionBankByParent{
    NSString *url = [NSString stringWithFormat:@"%@/questionBank/findQuestionBankByParent" ,[URLUtil hostName]];
    NSDictionary *params = @{@"parentId":[NSString stringWithFormat:@"%d",self.questionBankId]};
    [JZNRequestUtil requestUrl:url requestParams:params success:^(DataResult *dataResult) {
        if(dataResult.code == 0){
            self.deep++;
            NSArray *array = dataResult.data;
            if(array.count == 0&& self.deep>=1) {
                dispatch_async(dispatch_get_main_queue(), ^(){
                    QuestionBrowseViewController *vc = [[QuestionBrowseViewController alloc]init];
                    vc.questionBankId = self.questionBankId;
                    if(self.deep == 1){
                        [self.navigationController popViewControllerAnimated:NO];
                    }
                    [self.navigationController pushViewController:vc animated:YES];
                });
                return;
            }
           
            NSString *clear = [NSString stringWithFormat:@"getQuestionBankList(%@)",[dataResult.data mj_JSONString]];
            //[self.jznWebView stringByEvaluatingJavaScriptFromString: clear];
            [self.jznWebView evaluateJavaScript:clear completionHandler:^(id _Nullable data, NSError * _Nullable error) {
                
            }];
        }else {
            [self.navigationController popViewControllerAnimated:YES];
        }
    } failure:^(NSError *error) {
        
    }];
    
}


- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    [self findQuestionBankByParent];
}
-(void) findParentBankList{
    NSString *url = [NSString stringWithFormat:@"%@/questionBank/findParentBankList" ,[URLUtil hostName]];
    NSDictionary *params = @{@"parentId":[NSString stringWithFormat:@"%d",self.questionBankId]};
    self.deep--;
    [JZNRequestUtil requestUrl:url requestParams:params success:^(DataResult *dataResult) {
        if(dataResult.code == 0){

            NSString *clear = [NSString stringWithFormat:@"getQuestionBankList(%@)",[dataResult.data mj_JSONString]];
            NSArray *array = dataResult.data;
            self.questionBankId = [[array objectAtIndex:0][@"parentId"] intValue];
            [self.jznWebView evaluateJavaScript:clear completionHandler:^(id _Nullable data, NSError * _Nullable error) {
                
            }];
        }else {
        }
    } failure:^(NSError *error) {
    }];
}

-(void) goBack{
    if (self.deep>2) {
        [self findParentBankList];
        return;
    }
    [self.navigationController popViewControllerAnimated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
